package com.xqq.utils;

import org.springframework.web.util.UriComponentsBuilder;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;

public class Constants {
    public static final int DATA_TYPE_LOCATION = 1;
    public static final int DATA_TYPE_MESSAGE = 2;
    public static final String MESSAGE_SAFE = "2";
    public static final String MESSAGE_SOS = "3";
    public static final String KEY = "9VVQA88J5ZP4FMLZHH9EHJSP2";
    public static final String TIME_ZONE_SHANGHAI = "Asia/Shanghai";
    public static Map<String,String> DISORDER_TYPE_MAP;
    public static final int IS_DELETED = 1;
    public static final int NOT_DELETED = 0;
    public static final int NOT_CLEANED = 0;
    public static final int IS_WHITE = 1;
    public static final int IS_BLACK = 2;
    public static final int ABNORMOL = 1;
    public static final String DATE_PATTERN = "yyyy-MM-dd";
    static {
        //Strategic developments=1712
        //Battles=3127
        //Protests=145
        //Explosions/Remote violence=3271
        //Violence against civilians=952
        DISORDER_TYPE_MAP =new HashMap<>();
        DISORDER_TYPE_MAP.put("Strategic developments","军事行动");
        DISORDER_TYPE_MAP.put("Battles","武装冲突");
        DISORDER_TYPE_MAP.put("Protests","抗议游行");
        DISORDER_TYPE_MAP.put("Explosions/Remote violence","爆炸事件");
        DISORDER_TYPE_MAP.put("Violence against civilians","暴力事件");
    }

    //    public static double[] generateRandomCoordinates() {
    //        Random random = new Random();
    //        // 纬度范围：-90 到 90
    //        double latitude = round(random.nextDouble() * 180 - 90, 6);
    //        // 经度范围：-180 到 180
    //        double longitude = round(random.nextDouble() * 360 - 180, 6);
    //        return new double[]{longitude, latitude};
    //    }
    //
    //    private static double round(double value, int places) {
    //        double scale = Math.pow(10, places);
    //        return Math.round(value * scale) / scale;
    //    }
    public static void main(String[] args) throws Exception {
        // GitLab API URL
        String apiUrl = "https://gitlab.csnd.com/api/v4/projects/524/repository/files/src%2Fmain%2Fproto%2Fxqq-test14.proto10";
        // 你的 Private Token
        String privateToken = "JTE6wD7HzanznZqW18Qo";
        // 请求头和请求体数据
        String branch = "main";
        String authorEmail = "author@example.com";
        String authorName = "Firstname Lastname";
        String content = "some content"; // 文件内容
        String commitMessage = "create a new file";

        try {
            // 构建请求体 JSON
            String jsonData = "{"
                    + "\"branch\": \"" + branch + "\","
                    + "\"author_email\": \"" + authorEmail + "\","
                    + "\"author_name\": \"" + authorName + "\","
                    + "\"content\": \"" + content + "\","
                    + "\"commit_message\": \"" + commitMessage + "\""
                    + "}";

            // 创建 URL 对象
            URL url = new URL(apiUrl);
            // 打开连接
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            // 设置请求方式为 POST
            connection.setRequestMethod("POST");
            // 设置请求头
            connection.setRequestProperty("PRIVATE-TOKEN", privateToken);
            connection.setRequestProperty("Content-Type", "application/json");
            // 启用输入输出流
            connection.setDoOutput(true);

            // 发送请求体数据
            try (OutputStream os = connection.getOutputStream()) {
                byte[] input = jsonData.getBytes(StandardCharsets.UTF_8);
                os.write(input, 0, input.length);
            }

            // 获取响应状态
            int responseCode = connection.getResponseCode();
            if (responseCode == HttpURLConnection.HTTP_OK) {
                // 读取响应
                try (BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
                    String inputLine;
                    StringBuilder response = new StringBuilder();
                    while ((inputLine = in.readLine()) != null) {
                        response.append(inputLine);
                    }
                    // 打印响应内容
                    System.out.println("Response: " + response.toString());
                }
            } else {
                System.out.println("Failed to create file. HTTP Response Code: " + responseCode);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
